﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Attacker
{
    class Bit_reverse
    {
        private int[] tran = { 6, 7, 8, 9, 10, 11 };

        public void bit_attack(Database_mag data_mag,int col_mod,int bit_mod,int percentage)
        {
			data_mag.Backups();
            DataTable dt = data_mag.Read_table();
            int row_num = dt.Rows.Count;
            int num = row_num * percentage / 100;
            //bool[dt.Rows.Count] vis = { false };
            bool[] vis = new bool[row_num + 10];
            int cnt = 0;
            Random rd = new Random();
            int row = rd.Next(0, row_num - 1);
            while(cnt<num)
            {
                while(vis[row]==true)
                {
                    row = rd.Next(0, row_num - 1);
                }
                vis[row] = true;
                for(int i=0;i<col_mod;i++)
                {
                    string pre_val = dt.Rows[row][tran[i]].ToString();
                    int len = pre_val.Length;
                    string upd_val = pre_val.Substring(0, len - bit_mod);
                    for (int j = len - bit_mod; j < len; j++) 
                    {
                        int now = int.Parse(pre_val.Substring(j, 1));
                        now = (now + 1) % 10;
                        upd_val += now.ToString();
                    }
                    dt.Rows[row][tran[i]] = upd_val;
                }
                cnt++;
            }
			data_mag.Update_table();
		}
    }
}
